package GUI;

import 学生.Student;
import 数据库和网络.myDatebase;
import 老师.teacher;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class enterWindow extends JFrame{
    public static Student currentStudent;
    public static teacher currentTeacher;
    public static String identity;
    public  static JTextField IDText,PasswordText;
    JButton logButton,registerButton;
    public static JRadioButton stuOption,teaOption;
    JLabel label1,label2,IDLabel,PasswordLabel;
    //JFrame frame;

    public class LogListener implements ActionListener{
        //登录的Listener
        @Override
        public void actionPerformed(ActionEvent e) {
            String ID = IDText.getText();
            String password = PasswordText.getText();

                if (stuOption.isSelected()) {

                    currentStudent=new Student();
                    currentStudent.studentLog(ID,password);
                    enterWindow.this.dispose();
                }

                if (teaOption.isSelected()) {

                    currentTeacher=new teacher();
                    currentTeacher.teacherLog(ID,password);
                    enterWindow.this.dispose();

                }


        }
    }

    public class RegListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            enterWindow.this.dispose();
            new registerWindow();
        }
    }

    public class stuOptionListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            if(stuOption.isSelected()) {
                identity="学生";
                teaOption.setSelected(false);//保证同时只能有一个按钮被选中
            }
        }
    }
    public class teaOptionListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            if(teaOption.isSelected()){
                identity="老师";
                stuOption.setSelected(false);
            }
        }}

    public enterWindow(){
        JPanel panel = new JPanel(new GridLayout(5, 2,20,10));
        logButton = new JButton("登录");
        registerButton = new JButton("注册");
        stuOption= new JRadioButton("学生");
        teaOption= new JRadioButton("老师");
        logButton.addActionListener(new enterWindow.LogListener());
        registerButton.addActionListener(new enterWindow.RegListener());//用内部类增加多个ActionListener
        stuOption.addActionListener(new enterWindow.stuOptionListener());
        teaOption.addActionListener(new enterWindow.teaOptionListener());
        label1=new JLabel(" ");
        label2=new JLabel(" ");
        IDLabel=new JLabel("账号：");
        PasswordLabel=new JLabel("密码：");
        IDText = new JTextField(10);
        PasswordText= new JTextField(10);
        IDLabel.setHorizontalAlignment(SwingConstants.CENTER);
        PasswordLabel.setHorizontalAlignment(SwingConstants.CENTER);//设置Label文本居中
        stuOption.setHorizontalAlignment(SwingConstants.RIGHT);
        this.setSize(280,240);
        panel.setSize(this.getSize());
        logButton.setBackground(Color.lightGray);
        registerButton.setBackground(Color.lightGray);//设置按钮背景颜色
        stuOption.setSelected(true);
        panel.add(label1);
        panel.add(label2);
        panel.add(stuOption);
        panel.add(teaOption);
        panel.add(IDLabel);
        panel.add(IDText);
        panel.add(PasswordLabel);
        panel.add(PasswordText);
        panel.add(logButton);
        panel.add(registerButton);
        PasswordText.requestFocus();
        this.getContentPane().add(BorderLayout.CENTER,panel);
        this.setLayout(new FlowLayout());//流式布局
        this.setLocationRelativeTo(null);//居中
        this.setTitle("在线考试平台");
        this.setVisible(true);
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


}
